package com.example.myapplication;

import android.os.Bundle;
import android.os.Environment;
import android.text.format.Formatter;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.listener.OnItemChildClickListener;
import com.chad.library.adapter.base.listener.OnItemClickListener;
import com.example.myapplication.tools.GetAppInfoTools;

import java.util.AbstractCollection;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class ManagerListActivity extends AppCompatActivity {


    @BindView(R.id.tv_title)
    TextView tvTitle;
    @BindView(R.id.tv_space)
    TextView tvSpace;
    @BindView(R.id.tv_sdspace)
    TextView tvSDSpace;
    @BindView(R.id.tv_sofw_count)
    TextView tvSofwCount;
    private List<AppInfo> appList;

    @BindView(R.id.recyclerView)
    RecyclerView recyclerView;
    private AppAdapter adapter;
//    @BindView(R.id.swipeRefreshLayout)
//    SwipeRefreshLayout swipeRefreshLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_manager_list);
        // 注入的
        ButterKnife.bind(this);


        getSpaceInfo();

        appList = GetAppInfoTools.getAppInfoList(this);

        tvSofwCount.setText(appList.size()+"");

        initUI();
    }

    private void initUI() {
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        adapter = new AppAdapter(R.layout.item_appinfo,appList);
        recyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(@NonNull BaseQuickAdapter<?, ?> adapter1, @NonNull View view, int position) {
                for (int i = 0; i < appList.size(); i++) {
                    AppInfo info = appList.get(i);
                    if (info.isOpen()){
                        info.setOpen(false);
                        adapter.setData(i,info);
                    }
                }
                 AppInfo item = appList.get(position);
//                AppInfo item = (AppInfo)adapter.getItem(position);
                item.setOpen(true);
                adapter.setData(position,item);

            }
        });


    }

    private void getSpaceInfo() {
        long sdfreeSpace = Environment.getExternalStorageDirectory().getFreeSpace();
        long dataFreeSpace = Environment.getDataDirectory().getFreeSpace();

        String formatFileSize = Formatter.formatFileSize(this, sdfreeSpace);
        String formatFileSize1 = Formatter.formatFileSize(this, dataFreeSpace);

        tvSpace.setText(formatFileSize1);
        tvSDSpace.setText(formatFileSize);
    }




    @OnClick(R.id.iv_back)
    public void onViewClicked() {
        finish();
    }
}
